<?php
require_once '../includes/functions.php';
check_login();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改密码 - 落地页管理系统</title>
    <link rel="stylesheet" href="css/admin.css">
    <style>
        .password-form {
            max-width: 400px;
            margin: 40px auto;
            padding: 20px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: var(--text-color);
            font-weight: 500;
        }
        .form-group input {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
        .form-group input:focus {
            border-color: var(--primary-color);
            outline: none;
        }
        .error-message {
            color: #dc3545;
            font-size: 14px;
            margin-top: 4px;
            display: none;
        }
        .success-message {
            color: #28a745;
            font-size: 14px;
            margin-top: 4px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="admin-container">
        <div class="sidebar">
            <div class="sidebar-header">
                <h2>落地页管理系统</h2>
            </div>
            <ul class="sidebar-menu">
                <li><a href="index.php">仪表盘</a></li>
                <li><a href="create_subdomain.php">创建落地页</a></li>
                <li><a href="change_password.php" class="active">修改密码</a></li>
                <li><a href="logout.php">退出登录</a></li>
            </ul>
        </div>
        
        <div class="main-content">
            <div class="header">
                <h1>修改密码</h1>
                <div class="user-info">
                    欢迎, <?php echo htmlspecialchars($_SESSION['username']); ?>
                </div>
            </div>
            
            <div class="content">
                <div class="password-form">
                    <form id="passwordForm" onsubmit="return handleSubmit(event)">
                        <div class="form-group">
                            <label for="currentPassword">当前密码</label>
                            <input type="password" id="currentPassword" name="currentPassword" required>
                        </div>
                        <div class="form-group">
                            <label for="newPassword">新密码</label>
                            <input type="password" id="newPassword" name="newPassword" required>
                        </div>
                        <div class="form-group">
                            <label for="confirmPassword">确认新密码</label>
                            <input type="password" id="confirmPassword" name="confirmPassword" required>
                            <div class="error-message" id="errorMessage"></div>
                            <div class="success-message" id="successMessage"></div>
                        </div>
                        <button type="submit" class="btn btn-success" style="width: 100%;">修改密码</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
    async function handleSubmit(event) {
        event.preventDefault();
        
        const errorMessage = document.getElementById('errorMessage');
        const successMessage = document.getElementById('successMessage');
        errorMessage.style.display = 'none';
        successMessage.style.display = 'none';
        
        const currentPassword = document.getElementById('currentPassword').value;
        const newPassword = document.getElementById('newPassword').value;
        const confirmPassword = document.getElementById('confirmPassword').value;
        
        if (newPassword !== confirmPassword) {
            errorMessage.textContent = '两次输入的新密码不一致';
            errorMessage.style.display = 'block';
            return;
        }
        
        try {
            const response = await fetch('save_password.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    currentPassword,
                    newPassword
                })
            });
            
            const result = await response.json();
            
            if (result.success) {
                successMessage.textContent = '密码修改成功！';
                successMessage.style.display = 'block';
                document.getElementById('passwordForm').reset();
            } else {
                errorMessage.textContent = result.error || '密码修改失败';
                errorMessage.style.display = 'block';
            }
        } catch (error) {
            errorMessage.textContent = '发生错误，请稍后重试';
            errorMessage.style.display = 'block';
        }
        
        return false;
    }
    </script>
</body>
</html>
